package 力扣日常刷题.木23一月.第07天307;

import 力扣日常刷题.TreeNode;

import java.util.HashMap;
import java.util.HashSet;

/**
 * @author 帅小伙
 * @date 2022/3/7
 * @description
 */
public class Demo05根据描述创建二叉树 {


    public TreeNode createBinaryTree(int[][] descriptions) {
        //记录是否在第二个出现过次数
        HashSet<Integer> set = new HashSet<>();
        HashMap<Integer, TreeNode> map = new HashMap<Integer, TreeNode>();

        for (int[] description : descriptions) {
            int f = description[0],c = description[1],d = description[2];
            set.add(c);
            TreeNode parent = map.getOrDefault(f, new TreeNode(f));
            TreeNode child = map.getOrDefault(c, new TreeNode(c));
            if(d == 0)parent.right = child;
            if(d == 1)parent.left = child;
            map.put(f,parent);
            map.put(c,child);
        }

        for (int[] description : descriptions) {
            if(!set.contains(description[0])) return map.get(description[0]);
        }
        return null;

    }

}
